package org.smile.upload.http;

import java.io.IOException;
import java.io.InputStream;

public class UploadInputSteam extends InputStream
{

    private InputStream input = null;
    private StreamReport streamReport = null;

    public UploadInputSteam(InputStream input, StreamReport report)
    {
        this.input = input;
        this.streamReport = report;
    }

    public int read() throws IOException
    {
        byte b[] = new byte[1];
        int readNext = -1;
        int readByte = -1;
        if (input != null) readNext = input.read(b);
        if (readNext != -1)
        {
            readByte = b[0];
            streamReport.refresh(1L);
        }
        else
        {
            streamReport.refresh(-1L);
        }
        return readByte;
    }

    public int read(byte b[], int off, int len) throws IOException
    {
        int readSize = -1;
        if (input != null) readSize = input.read(b, off, len);
        streamReport.refresh(readSize);
        return readSize;
    }

    public void close() throws IOException
    {
        super.close();
        if (input != null) input.close();
    }

}
